<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="js/jquery.min.js" type="text/javascript"></script>
        <script src='js/jquery.md5.js' type="text/javascript"></script>
        <script src="js/bootstrap.min.js" type="text/javascript"></script>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <style>
            .file {
                opacity: 0; position: absolute; z-index: 999; top: 0; left: 0; bottom: 0; width:100%;
            }
        </style>
        <script type="text/javascript">
            var config = {
                local: {
                    baseUrl: 'http://localhost/ck/trunk/index.php',
                    appKey: 'DmI1UsPDkP-dIIDqLJw4lhNXPTcDOXGs'
                },
                dev: {
                    baseUrl: 'http://shop.iohuo.com/index.php',
                    appKey: 'DmI1UsPDkP-dIIDqLJw4lhNXPTcDOXGs'
                }
            };

            $(function () {
                $('#addFile').click(function(){
                    var filename = $('#filename').val();
                    var obj = $('<div class="input-group"><span class="input-group-btn">' + 
                            '<button type="button" class="delete btn btn-danger">删除</button>' +
                            '</span><input type="text" name="param" class="form-control">' +
                            '<span class="input-group-btn">' +
                            '<input type="file" name="'+ filename +'" class="file">' +
                            '<button type="button" id="choose" class="btn btn-primary">浏览</button>' +
                        '</span></div>');
                    obj.find('input[type=file]').change(function(){
                        var _this = $(this);
                        if(_this.val() !== '') {
                            var type = $('#type').val();
                            var baseUrl = config[type]['baseUrl'] + '?d=api&c=test&m=fileMd5';
                            var appKey = config[type]['appKey'];
                            var fd = new FormData();
                            fd.append(_this.attr('name'), _this[0].files[0]);
                            var data = new Array();
//                            debugger;
                            sign(fd, data, appKey);
                            $.ajax({
                               url: baseUrl,
                               type: 'post',
                               data: fd,
                               dataType: 'json',
                               processData: false,
                               contentType: false,
                               success: function(data) {
                                    if(data.resultCode === '11100') {
                                        _this.parent().prev().val('_=' + data.md5);
                                        _this.next().addClass('btn-success');
                                    }
                                    json(data, 1);
                               },
                               error: function(data) {
                                   json(data.responseText, 0);
                               }
                            });
                        } else {
                            _this.next().removeClass('btn-success');
                        }
                    });
                    obj.find('.delete').click(function () {
                        obj.remove();
                    });
                    obj.insertBefore($(this));
                });
                
                
                $('#addElement').click(function () {
                    var obj = $('<div class="input-group">' +
                            '<input type="text" name="param" class="form-control">' +
                            '<span class="input-group-btn">' +
                            '<button type="button" class="btn btn-danger delete">删除</button>' +
                            '</span></div>');
                    obj.find('.delete').click(function () {
                        obj.remove();
                    })
                    obj.insertBefore($(this));
                });

                var sign = function(fd, data, appKey) {
                    var timestamp = new Date().getTime();
                    data.push(timestamp);
                    fd.append('timestamp', timestamp);
                    
                    data.sort();

                    var valueString = '';
                    for(var key in data) {
                        valueString += data[key];
                    }
                    console.log(valueString);
                    valueString += appKey;
                    var signString = $.md5(valueString);
                    fd.append('sign', signString);
                    
                    return fd;
                }

                var json = function(text, isJson) {
                    $('#json').html('<br/>');
                    setTimeout(function(){
                        $('#json').html((isJson === 1)?JSON.stringify(text):text);
                    }, 1000);
                }
                
                $('#login').click(function(){
                    var type = $('#type').val();
                    var loginObj = $('#loginObj').val();
                    var baseUrl = config[type]['baseUrl'] + '?d=api&c='+loginObj+'&m=login';
                    var appKey = config[type]['appKey'];
                    
                    var fd = new FormData();
                    var obj = $(this).siblings('input[data-validate=required]');
                    var data = new Array();
//                    debugger;

                    $.each(obj, function(){
                        var item = $(this);
                        fd.append(item.attr('name'), item.val());
                        data.push(item.val());                      
                    });
                    
                    sign(fd, data, appKey);
                    
                    $.ajax({
                        url: baseUrl,
                        type: 'post',
                        data: fd,
                        dataType: 'json',
                        processData: false,
                        contentType: false,
                        success: function(data) {
                            if(data.resultCode === '11100')
                                $('#token').val(data.data.access_token);
                            json(data, 1);
                        },
                        error: function(data) {
                            json(data.responseText, 0);
                        }
                    });
                });
                
                $('#commit').click(function(){
                    var router = $('input[name=url]').val();
                    var type = $('#type').val();
                    var baseUrl = config[type]['baseUrl'] + "?" + router;
                    var appKey = config[type]['appKey'];
                    
                    var fd = new FormData();
                    var obj = $('#paramForm').find('input[name=param]');
                    var file = $('#paramForm').find('input[type=file]');
                    var data = new Array();
                    
                    $.each(obj, function(){
                        var item = $(this);
                        var value = item.val();
                        if(value === '') {
                            return true;
                        }
                        index = value.indexOf('=');
                        if(index === -1) {
                            return true;
                        }
                        var name = value.substr(0, index);
                        var val = value.substring(index+1);
                        fd.append(name, val);
                        data.push(val);
                    });
                    var token = $('#token').val();
                    fd.append('access_token', token);
                    data.push(token);     
//debugger;
                    sign(fd, data, appKey);               
                    $.each(file, function(){
                        if(file.val() !== '') {
                            fd.append($(this).attr('name'), $(this)[0].files[0]);
                        }
                    });

                    $.ajax({
                        url: baseUrl,
                        type: 'post',
                        data: fd,
                        dataType: 'json',
                        processData: false,
                        contentType: false,
                        success: function(data) {
                            json(data, 1);
                        },
                        error: function(data) {
                            json(data.responseText, 0);
                        }
                    });               
                });
            });
        </script>
    </head>
    <body>
        <div style="padding:10px;">
            <div class="form-inline">
                <label>类型：</label><input type="text" value="local" class="form-control" id='type'>
                <label>Token：</label><input type="text" value="" class="form-control" id='token'>
            </div>
            <br/>
            <div class="form-inline">
                <form id='LoginForm' name='LoginForm'>
                    <label>接口：</label><input type="text" id="loginObj" value="users" class="form-control"> 
                    <label>账号：</label><input type="text" name="username" data-validate="required" value="15711550782" class="form-control"> 
                    <label>密码：</label><input type="password" name="password" data-validate="required" value="123456" class="form-control"> 
                    <button type='button' class="btn btn-primary" id="login">登陆</button>
                </form>
            </div>
            <br/>
            <div class="form-inline">
                <form id='paramForm'>
                    <label>参数：</label><input type="text" name="url" value="d=api&c=users&m=login" class="form-control">
                    <button type='button' class="btn btn-primary" id="addElement">添加参数</button>
                    <button type='button' class="btn btn-primary" id="addFile">添加文件</button>
                    <input type="text" size="6" name="filename" id="filename" value="filedata[]" class="form-control">
                </form>
            </div>
            <br/>
            <div class="form-inline"><button class="btn btn-success" id="commit">提交</button></div>
            <br/>
            <div class='form-inline  col-sm-6'><div id='json' class="alert alert-warning" role="alert"><br/></div></div>
        </div>
    </body>
</html>






getStock($address_id, $config, $type)

where_in('order_id', array_keys($tmp1))



    $query = $this->db->select ( 'p.id,p.name,format(p.price/100,2) price,s.stock,p.specs,a.img',false )->from ( 'product p' )->where ( array (
                'p.status' => '10'
        ) )->join ( 'album a', 'p.thumbnail_id=a.id', 'left' )->join ( 'sm_warehouse_stock s', 'p.id=s.product_id', 'left' )
        ->get ();$this->identity ['address_id']